<?php
    function set_ext_dir(&$cdrs, $ext){
        //$dirs = array();
        $sz = count($cdrs);
        for ($i=0; $i<$sz; $i++){
            if (in_array($cdrs[$i]['ocg'], $ext) && !in_array($cdrs[$i]['ocd'], $ext))
                $cdrs[$i]['dir'] = 0; //outgoing
            elseif (in_array($cdrs[$i]['ocd'], $ext) && !in_array($cdrs[$i]['ocg'], $ext))
                $cdrs[$i]['dir'] = 1; //incoming
            else
                $cdrs[$i]['dir'] = 2; //internal calls (within same department/branch)
        }
        //return $dirs;
    }
    
    require_once 'mysql_db.php';
    $db = new Db();
    $cdrs = NULL;
    $ext_list = NULL;
    
    if (isset($_GET['ext']) && $_GET['ext']!=''){
        $ext = $_GET['ext'];
        $cdrs = $db->get_ext_cdrs($ext);
        $ext_list = array($ext);
        //array_push($cdrs, $dirs);
    }
    else if (isset ($_GET['dep']) && $_GET['dep']!=''){
        $dep_id = $_GET['dep'];
        $cdrs = $db->get_dep_cdrs($dep_id);
        $ext_list = $db->get_dep_ext($dep_id);
    }
    else if (isset ($_GET['emp']) && $_GET['emp']!=''){
        $emp_id = $_GET['emp'];
        $cdrs = $db->get_emp_cdrs($emp_id);
        $ext_list = $db->get_emp_ext($emp_id);
    }
    else{
        $cdrs = $db->get_all_cdrs();
    }
    
    set_ext_dir($cdrs, $ext_list);
    echo json_encode($cdrs);
?>
